package com.semlab.server.graph;

import javax.annotation.PostConstruct;

import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.index.Index;
import org.neo4j.graphdb.index.IndexHits;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.social.facebook.api.FacebookProfile;
import org.springframework.stereotype.Component;

import com.semlab.server.resources.Profile;
import com.semlab.server.resources.RelationshipTypes;
import com.semlab.server.resources.SLog;
import com.semlab.shared.NamespaceConstants;

@Component
public class ProfileRepository extends Repository<Profile>{

    private final Logger log = LoggerFactory.getLogger(getClass());

	private Index<Node> profiles;	
	public ProfileRepository() {
	}
	
	@PostConstruct
	public void init() {
		profiles = template.getIndex(PROFILE);
	}

	public void addProfile(Profile profile) {
//		log.debug("adding profile: "+profile);
		profiles.add(template.getNode(profile.getNodeId()), NamespaceConstants.CONCEPT_ID,
				profile.getId());
	}

	public Profile getById(String id) {
		IndexHits<Node> hits = profiles.get(NamespaceConstants.CONCEPT_ID, id);
		Node profile = null;
		if (hits.hasNext()) {
			profile = hits.next();
    		log.debug("Index found profile with id: "+profile.getId());
		}
		return (profile != null) ? getByNode(profile) : null;
	}
	
	@Override
	public Node persist(Profile profile) {
		Node node = null;
		if(profile.getId() != null){
			node = getDomainNode(profile.getId());
		}
		if(node == null){
			node = template.createNode();
			profile.setNodeId(node.getId());
			if(profile.getId() != null)
				addProfile(profile);
		}
		
		
		profile.setNodeId(node.getId());
		template.setNodeProperty(node, NamespaceConstants.CONCEPT_ID, profile.getId());
		template.setNodeProperty(node, NamespaceConstants.PROFILE_NAME, profile.getName());
		template.setNodeProperty(node, NamespaceConstants.TYPE, profile.getType());
		template.setNodeProperty(node, NamespaceConstants.FIRST_NAME, profile.getFirst_name());
		template.setNodeProperty(node, NamespaceConstants.LAST_NAME, profile.getLast_name());
		template.setNodeProperty(node, NamespaceConstants.PROFILE_URL, profile.getProfile_url());
		template.setNodeProperty(node, NamespaceConstants.USERNAME, profile.getUsername());
		template.setNodeProperty(node, NamespaceConstants.BIRTHDAY, profile.getBirthday());
		template.setNodeProperty(node, NamespaceConstants.GENDER, profile.getGender());
		template.setNodeProperty(node, NamespaceConstants.EMAIL, profile.getEmail());
		template.setNodeProperty(node, NamespaceConstants.WEBSITE, profile.getWebsite());
        template.setNodeProperty(node, NamespaceConstants.IMPORTED, profile.isImported());
		
		return node;
	}

	@Override
	public Profile getByNode(Node node) {
		if(node == null)
			return null;
		Profile profile = new Profile();
		profile.setNodeId(node.getId());
        profile.setImported(Boolean.parseBoolean(node.getProperty(NamespaceConstants.IMPORTED).toString()));
		return profile;
	}

	@Override
	public Node getDomainNode(String id) {
		log.debug(" id= "+id);
		IndexHits<Node> hits = profiles.get(NamespaceConstants.CONCEPT_ID, id);
		Node profile = null;
		if (hits.hasNext()) {
			profile = hits.next();
    		log.debug(" Index found profile with id: "+profile.getId()+" ");
		}
		return (profile != null) ? profile : null;
	}

	public Profile parseFacebookProfile(FacebookProfile profile){
		Profile person = new Profile();
		person.setId(profile.getId());
		person.setFirst_name(profile.getFirstName());
//		person.setLast_name(profile.getLastName());
		if(profile.getBirthday() != null)
			person.setBirthday(person.parseBirthday(profile.getBirthday()));
		
//		person.setEmail(profile.getEmail());
		person.setGender(profile.getGender());
//		person.setName(profile.getName());
//		person.setProfile_url(profile.getLink());
		person.setWebsite(profile.getWebsite());
		return person;
	}
	
	public void makeFriends(Profile p1, Profile p2){
//		log.debug("making friend");
		Node n1 = template.getNode(p1.getNodeId());
		Node n2 = template.getNode(p2.getNodeId());
		template.relate(n1, n2, RelationshipTypes.FRIEND);
	}


}
